		 **************************************************************
		 **															 **
		 ** Paper: Freedom of Foreign Movement and Human Trafficking **
		 **														     **
		 ** Authors: Sam R. Bell & Richard W. Frank 				 **
		 **															 **
		 ** Date: May 11, 2021										 **
		 **															 **
		 ** Task: Merging datasets for analysis						 **
		 **															 **
		 **************************************************************
 
 
		clear
		version 16.1
		set seed 1234
		set more off, perm
		clear matrix
		clear mata

		** change working directory **
		cd "~"
				 
					***************************
					** merging data sources ***
					***************************
		
		**************************
		** 1. starting with HTI **
		** DV: traff. source    **
		**************************
			
			use "HTI 00-17.dta", clear
			drop if ccode<0
			keep country ccode year source destination internal psource pdest pinternal ///
				 ldest lsource linternal
			save temp1.dta, replace
		
		***********************************
		** 2. CI-Rights foreign movement **
		***********************************
		
			use"CIRIGHTS 2019.dta", clear
			keep formov dommov ccode year
			tab formov 
			tab dommov
			tab year if formov~=.
			collapse (mean) formov dommov , by(ccode year)
			save temp2.dta, replace	
		
		*******************
		** 3. World Bank **
		** gdp; pop *******
		*******************
		
			use "WDI_2020.dta", clear
			run "CCODE to name.do"
			run "Creating World Bank Region Codes.do"
			keep ccode year gdpconstant2000 population
			save temp3.dta, replace
			
		****************
		** 4. POLITY2 **
		****************	
		
			use  "Polity 2018.dta", clear
			keep ccode year polity2
			save temp4.dta, replace
		
		****************************************
		** 5. KOF globalization overall index **
		****************************************		
			
			use "KOFGI_2019_data.dta", clear
			keep country year KOFGI   
			rename country Country
			run "cow.do"
			sort ccode year
			tab Country if ccode==0
			browse if ccode==ccode[_n-1] & year==year[_n-1]
			browse if ccode==710
			drop if ccode==0
			collapse (min)  KOFGI, by(ccode year)
			label var KOFGI "Overall globalization index (KOF)"
			save temp5.dta, replace 
		
		*****************
		** 6. 3P index **
		*****************
		
			use "data3P_2015.dta", clear
			drop code Country
			save temp6.dta, replace 	
				
		**************
		** 7. V-DEM **
		**************
			
			use "/vdem_move.dta", clear
			rename COWcode ccode
			keep ccode year v2cldmovem v2cldmovew v2clfmove v2clslavef v2clslavem v2xcl_dmove v2xcl_slave
			drop if year<1999
			drop if ccode==.
			save temp7.dta, replace 
		 
		 **********
		 ** CIRI **
		 **********
		 
		 use "CIRI 2011.dta", clear
		 rename cow ccode
		 keep ccode year dommov formov
		 rename formov fm
		 replace fm=. if fm==-77
		 replace fm=. if fm==-66
		 replace fm=. if fm==-999		 
		 rename dommov dm
		 drop if year<1999		 
		 save temp8.dta, replace 		 
		 
		 
							****************** 
							** MERGING DATA **
							******************
			
			use temp1.dta, clear
			merge 1:1 ccode year using temp2.dta
			drop _merge
			merge 1:1 ccode year using temp3.dta
			drop _merge
			merge 1:1 ccode year using temp4.dta	
			drop _merge		
			merge 1:1 ccode year using temp5.dta	
			drop _merge		
			merge 1:1 ccode year using temp6.dta	
			drop _merge		
			merge 1:1 ccode year using temp7.dta		
			drop _merge		
			merge 1:1 ccode year using temp8.dta		
			drop _merge			
			keep if year>1998
				
			drop if year==2019	
			order ccode country year	
			sort ccode year	
			tsset ccode year

 
							*******************
							** CLEANING DATA **
							*******************
		
		 ** EU control**
	
			gen EU=0
			replace EU=1 if ccode==211
			replace EU=1 if ccode==220
			replace EU=1 if ccode==255
			replace EU=1 if ccode==325
			replace EU=1 if ccode==212
			replace EU=1 if ccode==210
			replace EU=1 if ccode==390
			replace EU=1 if ccode==205
			replace EU=1 if ccode==200
			replace EU=1 if ccode==350
			replace EU=1 if ccode==235
			replace EU=1 if ccode==230
			replace EU=1 if ccode==305
			replace EU=1 if ccode==375
			replace EU=1 if ccode==380
			replace EU=1 if ccode==352 & year>2003
			replace EU=1 if ccode==316 & year>2003
			replace EU=1 if ccode==366 & year>2003
			replace EU=1 if ccode==310 & year>2003
			replace EU=1 if ccode==367 & year>2003
			replace EU=1 if ccode==368 & year>2003
			replace EU=1 if ccode==338 & year>2003
			replace EU=1 if ccode==290 & year>2003
			replace EU=1 if ccode==317 & year>2003
			replace EU=1 if ccode==349 & year>2003
			replace EU=1 if ccode==355 & year>2006
			replace EU=1 if ccode==360 & year>2003
				
		** logging necessary variables **
		
		replace gdp=. if gdp==0
		gen gdp_ln = ln(gdp)
		label var gdp_ln "GDP, ln (constant 2000USD)"
		
		gen pop_ln = ln(pop)
		label var pop_ln "Population, ln"
		
		/*
		hist KOFGI, freq /* fine */
		hist dommov, freq /* fine */
		hist formov, freq /* fine */
		hist overall3p, freq
		hist polity2, freq
		hist prevention, freq
		hist prosecution, freq
		hist protection, freq
		hist v2cldmovem, freq
		hist v2cldmovew, freq
		hist v2clfmove, freq
		hist v2clslavef, freq
		hist v2clslavem, freq
		hist v2xcl_dmove, freq
		hist v2xcl_slave, freq */
		
		save temp9.dta, replace
		keep ccode year fm dm dommov formov v2cldmovem v2cldmovew v2clfmove v2clslavef v2clslavem v2xcl_slave	 
		save temp10.dta, replace
		
	**************************************
	** Adding in the spatial statistics **
	**************************************							

		use "contiguity.dta", clear
		sort ccode2 year
		drop  _merge  
		rename ccode2 ccode
		codebook year
		drop if year<1999
		sort ccode1 ccode year
		drop dyad version
		expand 6 if year==2012
		sort ccode1 ccode year
		by ccode1 ccode: replace year=year+1 if year==2012 & year[_n-2]==2011
		by ccode1 ccode: replace year=year+2 if year==2012 & year[_n-3]==2011
		by ccode1 ccode: replace year=year+3 if year==2012 & year[_n-4]==2011
		by ccode1 ccode: replace year=year+4 if year==2012 & year[_n-5]==2011
		by ccode1 ccode: replace year=year+5 if year==2012 & year[_n-6]==2011
						
		merge m:1 ccode year using "temp10.dta"
		drop  _merge  
		rename ccode ccode2
		set more off
		
		** CIRIGHTS foreign movement **
		spmon  formov, weight(contig) i(ccode1) k(ccode2) time(year) sename(W_fomov_con) filename(spatialmovement) 
		spmon  formov, weight(contig_or_less_12m) i(ccode1) k(ccode2) time(year) sename(W_fomov_con12) filename(spatialmovement) 
		spmon  formov, weight(contig_or_less_24m) i(ccode1) k(ccode2) time(year) sename(W_fomov_con24) filename(spatialmovement) 
		spmon  formov, weight(contig_or_less_150) i(ccode1) k(ccode2) time(year) sename(W_fomov_con150) filename(spatialmovement)
		spmon  formov, weight(contig_or_less_400m) i(ccode1) k(ccode2) time(year) sename(W_fomov_con400) filename(spatialmovement) 
		 
		** CIRIGHTS domestic movement ** 
		spmon  dommov, weight(contig) i(ccode1) k(ccode2) time(year) sename(W_dommov_con) filename(spatialmovement) 
		spmon  dommov, weight(contig_or_less_12m) i(ccode1) k(ccode2) time(year) sename(W_dommov_con12) filename(spatialmovement) 
		spmon  dommov, weight(contig_or_less_24m) i(ccode1) k(ccode2) time(year) sename(W_dommov_con24) filename(spatialmovement) 
		spmon  dommov, weight(contig_or_less_150) i(ccode1) k(ccode2) time(year) sename(W_dommov_con150) filename(spatialmovement)
		spmon  dommov, weight(contig_or_less_400m) i(ccode1) k(ccode2) time(year) sename(W_dommov_con400) filename(spatialmovement) 

		** CIRI foreign movement **
		spmon  fm, weight(contig) i(ccode1) k(ccode2) time(year) sename(W_fm_con) filename(spatialmovement) 
		spmon  fm, weight(contig_or_less_12m) i(ccode1) k(ccode2) time(year) sename(W_fm_con12) filename(spatialmovement) 
		spmon  fm, weight(contig_or_less_24m) i(ccode1) k(ccode2) time(year) sename(W_fm_con24) filename(spatialmovement) 
		spmon  fm, weight(contig_or_less_150) i(ccode1) k(ccode2) time(year) sename(W_fm_con150) filename(spatialmovement)
		spmon  fm, weight(contig_or_less_400m) i(ccode1) k(ccode2) time(year) sename(W_fm_con400) filename(spatialmovement) 
		 
		** CIRI domestic movement ** 
		spmon  dm, weight(contig) i(ccode1) k(ccode2) time(year) sename(W_dm_con) filename(spatialmovement) 
		spmon  dm, weight(contig_or_less_12m) i(ccode1) k(ccode2) time(year) sename(W_dm_con12) filename(spatialmovement) 
		spmon  dm, weight(contig_or_less_24m) i(ccode1) k(ccode2) time(year) sename(W_dm_con24) filename(spatialmovement) 
		spmon  dm, weight(contig_or_less_150) i(ccode1) k(ccode2) time(year) sename(W_dm_con150) filename(spatialmovement)
		spmon  dm, weight(contig_or_less_400m) i(ccode1) k(ccode2) time(year) sename(W_dm_con400) filename(spatialmovement) 

		** VDEM foreign movement 
		spmon  v2clfmove, weight(contig) i(ccode1) k(ccode2) time(year) sename(W_vfomov_con) filename(spatialmovement) 
		spmon  v2clfmove, weight(contig_or_less_12m) i(ccode1) k(ccode2) time(year) sename(W_vfomov_con12) filename(spatialmovement) 
		spmon  v2clfmove, weight(contig_or_less_24m) i(ccode1) k(ccode2) time(year) sename(W_vfomov_con24) filename(spatialmovement) 
		spmon  v2clfmove, weight(contig_or_less_150) i(ccode1) k(ccode2) time(year) sename(W_vfomov_con150) filename(spatialmovement)
		spmon  v2clfmove, weight(contig_or_less_400m) i(ccode1) k(ccode2) time(year) sename(W_vfomov_con400) filename(spatialmovement) 
				
		** VDEM domestic movement -men **
		spmon  v2cldmovem, weight(contig) i(ccode1) k(ccode2) time(year) sename(W_vdommovm_con) filename(spatialmovement) 
		spmon  v2cldmovem, weight(contig_or_less_12m) i(ccode1) k(ccode2) time(year) sename(W_vdommovm_con12) filename(spatialmovement) 
		spmon  v2cldmovem, weight(contig_or_less_24m) i(ccode1) k(ccode2) time(year) sename(W_vdommovm_con24) filename(spatialmovement) 
		spmon  v2cldmovem, weight(contig_or_less_150) i(ccode1) k(ccode2) time(year) sename(W_vdommovm_con150) filename(spatialmovement)
		spmon  v2cldmovem, weight(contig_or_less_400m) i(ccode1) k(ccode2) time(year) sename(W_vdommovm_con400) filename(spatialmovement) 
			
		** VDEM domestic movement -women **
		spmon  v2cldmovew, weight(contig) i(ccode1) k(ccode2) time(year) sename(W_vdommovw_con) filename(spatialmovement) 
		spmon  v2cldmovew, weight(contig_or_less_12m) i(ccode1) k(ccode2) time(year) sename(W_vdommovw_con12) filename(spatialmovement) 
		spmon  v2cldmovew, weight(contig_or_less_24m) i(ccode1) k(ccode2) time(year) sename(W_vdommovw_con24) filename(spatialmovement) 
		spmon  v2cldmovew, weight(contig_or_less_150) i(ccode1) k(ccode2) time(year) sename(W_vdommovw_con150) filename(spatialmovement)
		spmon  v2cldmovew, weight(contig_or_less_400m) i(ccode1) k(ccode2) time(year) sename(W_vdommovw_con400) filename(spatialmovement) 
 	
		save "spatialmovement", replace
		
		keep ccode1 year W_dm* W_fm* W_dommov_co* W_fomov_co* W_vdommovm_co* W_vdommovw_co* W_vfomov_co*
		collapse (mean) W_dm* W_fm* W_dommov_co* W_fomov_co* W_vdommovm_co* W_vdommovw_co* ///
				W_vfomov_co*, by (ccode1 year)
		rename ccode1 ccode
		sort ccode year

		save "spatialmovment_collapsed", replace

					
		***********************************	
		** 10. Other trafficking sources **
		***********************************			
		
		** IOM 2000-2012 **
		
			use "IOM TIP 2000-2012.dta", clear
			
			keep ccode year victims_iom sextrafficking_iom labortrafficking_iom
			save temp11.dta,replace
		
		** UNODC 03-15 **
			 
			use	"UNODC_cyr_03_15.dta", clear
				
			keep ccode year victims victims_labor victims_sex
			save temp12.dta, replace
		
		** GSI **

			use "GSI 13-18.dta", clear
			replace year=year-1
			save temp13.dta, replace
			
		***************************
		*merge into existing data *
		***************************
			
			use "temp9.dta", clear
			sort ccode year
			merge 1:1 ccode year using "spatialmovment_collapsed"
			drop _merge
			merge 1:1 ccode year using "temp11.dta"
			drop _merge
			merge 1:1 ccode year using "temp12.dta"
			drop _merge		
			merge 1:1 ccode year using "temp13.dta"
			drop _merge		
			
			drop if ccode==.
			drop if year<1999
			drop if year>2017
			
			sort ccode year
			by ccode: replace country=country[_n-1] if country=="" & country[_n-1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""
			by ccode: replace country=country[_n-1] if country=="" & country[_n-1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""
			by ccode: replace country=country[_n+1] if country=="" & country[_n+1] ~=""

		******************************
		*** V-DEM interaction terms **
		******************************
			
			** Foreign movement **
			
				gen interact_W_Vf = v2clfmove*W_vfomov_con
				label var interact_W_Vf "V-DEM foreign interaction (contig)"
				
				gen interact_W_Vf12 = v2clfmove*W_vfomov_con12
				label var interact_W_Vf12 "V-DEM foreign interaction (12km)"
					
				gen interact_W_Vf24 = v2clfmove*W_vfomov_con24
				label var interact_W_Vf24 "V-DEM foreign interaction (24km)"
					 
				gen interact_W_Vf150 = v2clfmove*W_vfomov_con150
				label var interact_W_Vf150 "V-DEM foreign interaction (150km)"
					 
				gen interact_W_Vf400 = v2clfmove*W_vfomov_con400
				label var interact_W_Vf400 "V-DEM foreign interaction (400km)"
					 

			** Domestic movement - men**
			
				gen interact_W_Vdm = v2cldmovem*W_vdommovm_con
				label var interact_W_Vdm "V-DEM domestic men interaction (contig)"
				
				gen interact_W_Vdm12 = v2cldmovem*W_vdommovm_con12
				label var interact_W_Vdm12 "V-DEM domestic men interaction (12km)"
					
				gen interact_W_Vdm24 = v2cldmovem*W_vdommovm_con24
				label var interact_W_Vdm24 "V-DEM domestic men interaction (24km)"
					 
				gen interact_W_Vdm150 = v2cldmovem*W_vdommovm_con150
				label var interact_W_Vdm150 "V-DEM domestic men interaction (150km)"
					 
				gen interact_W_Vdm400 = v2cldmovem*W_vdommovm_con400
				label var interact_W_Vdm400 "V-DEM domestic men interaction (400km)"
			
			** Domestic movement - women**
			
				gen interact_W_Vdw = v2cldmovem*W_vdommovw_con
				label var interact_W_Vdw "V-DEM domestic women interaction (contig)"
				
				gen interact_W_Vdw12 = v2cldmovem*W_vdommovw_con12
				label var interact_W_Vdw12 "V-DEM domestic women interaction (12km)"
					
				gen interact_W_Vdw24 = v2cldmovem*W_vdommovw_con24
				label var interact_W_Vdw24 "V-DEM domestic women interaction (24km)"
					 
				gen interact_W_Vdw150 = v2cldmovem*W_vdommovw_con150
				label var interact_W_Vdw150 "V-DEM domestic women interaction (150km)"
					 
				gen interact_W_Vdw400 = v2cldmovem*W_vdommovw_con400
				label var interact_W_Vdw400 "V-DEM domestic women interaction (400km)"
 
		*********************************
		*** CIRIGHTS interaction terms **
		*********************************
 
			** Foreign movement **
			
				gen interact_W_CIRI_f = formov*W_fomov_con
				label var interact_W_CIRI_f "CIRIGHTS foreign interaction (contig)"
				
				gen interact_W_CIRI_f12 = formov*W_fomov_con12
				label var interact_W_CIRI_f12 "CIRIGHTS foreign interaction (12km)"
					
				gen interact_W_CIRI_f24 = formov*W_fomov_con24
				label var interact_W_CIRI_f24 "CIRIGHTS foreign interaction (24km)"
					 
				gen interact_W_CIRI_f150 = formov*W_fomov_con150
				label var interact_W_CIRI_f150 "CIRIGHTS foreign interaction (150km)"
					 
				gen interact_W_CIRI_f400 = formov*W_fomov_con400
				label var interact_W_CIRI_f400 "CIRIGHTS foreign interaction (400km)"
	 	
			** Domestic movement **
			
				gen interact_W_CIRI_d = dommov*W_dommov_con
				label var interact_W_CIRI_d "CIRIGHTS domestic interaction (contig)"
				
				gen interact_W_CIRI_d12 = dommov*W_dommov_con12
				label var interact_W_CIRI_d12 "CIRIGHTS domestic interaction (12km)"
					
				gen interact_W_CIRI_d24 = dommov*W_dommov_con24
				label var interact_W_CIRI_d24 "CIRIGHTS domestic interaction (24km)"
					 
				gen interact_W_CIRI_d150 = dommov*W_dommov_con150
				label var interact_W_CIRI_d150 "CIRIGHTS domestic interaction (150km)"
					 
				gen interact_W_CIRI_d400 = dommov*W_dommov_con400
				label var interact_W_CIRI_d400 "CIRIGHTS domestic interaction (400km)"
	 		 
		
		*********************************
		*** CIRI interaction terms **
		********************************
			
			** Foreign movement **
			
				gen interact_W_C_f = fm*W_fm_con
				label var interact_W_C_f "CIRI foreign interaction (contig)"
				
				gen interact_W_C_f12 = fm*W_fm_con12
				label var interact_W_C_f12 "CIRI foreign interaction (12km)"
					
				gen interact_W_C_f24 = fm*W_fm_con24
				label var interact_W_C_f24 "CIRI foreign interaction (24km)"
					 
				gen interact_W_C_f150 = fm*W_fm_con150
				label var interact_W_C_f150 "CIRI foreign interaction (150km)"
					 
				gen interact_W_C_f400 = fm*W_fm_con400
				label var interact_W_C_f400 "CIRI foreign interaction (400km)"
	 	
			
			** Domestic movement **
			
			
				gen interact_W_C_d = dm*W_dm_con
				label var interact_W_C_d "CIRI domestic interaction (contig)"
				
				gen interact_W_C_d12 = dm*W_dm_con12
				label var interact_W_C_d12 "CIRI domestic interaction (12km)"
					
				gen interact_W_C_d24 = dm*W_dm_con24
				label var interact_W_C_d24 "CIRI domestic interaction (24km)"
					 
				gen interact_W_C_d150 = dm*W_dm_con150
				label var interact_W_C_d150 "CIRI domestic interaction (150km)"
					 
				gen interact_W_C_d400 = dm*W_dm_con400
				label var interact_W_C_d400 "CIRI domestic interaction (400km)"
	 
		
		save "ht_merged.dta", replace
		
			erase temp1.dta
			erase temp2.dta		
			erase temp3.dta		
			erase temp4.dta		
			erase temp5.dta		
			erase temp6.dta		
			erase temp7.dta		
			erase temp8.dta
			erase temp9.dta
			erase temp10.dta
			erase temp11.dta		
			erase temp12.dta		
			erase temp13.dta		
		
